﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PipeAuto.DAL.PipeAutoDataSetTableAdapters;
using System.Security.Cryptography;


namespace PipeAuto.MgrPlatform
{
    public static class MgrPlatformGlobal
    {
        /// <summary>
        /// records operation state
        /// </summary>
        public enum OperationState
        {
            Null=0,
            Add=1,
            Update=2
        }

        public static TreeNode LeveledRecurseTreeView(string nodeID, TreeNode startNode, int level=-1)
        {

            //ignore level
            if (level == -1)
            {
                if (startNode == null)
                {
                    return null;
                }

                if (startNode.Tag.ToString() == nodeID)
                {
                    return startNode;
                }
            }
            else
            {


                //leaf node
                if (startNode.Level == level)
                {
                    //the right node
                    if (startNode.Tag.ToString() == nodeID)
                    {
                        return startNode;
                    }

                    return null;
                }
            }

            TreeNode myRetNode = null;

            for (int i = 0; i < startNode.Nodes.Count; i++)
            {
                myRetNode = LeveledRecurseTreeView(nodeID, startNode.Nodes[i],level);
                if (myRetNode != null)
                {
                    break;
                }
            }

            return myRetNode;
        }


        public static string GetSequence(string tableName, string columnName)
        {
            SequencesTableAdapter myAdapter = new SequencesTableAdapter();
            var mytable = myAdapter.GetDataByKey(tableName, columnName);

            var mySeqDate = DateTime.ParseExact(mytable[0].CurrentSequence.Substring(0, 8), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);

            if (mySeqDate!=DateTime.Now.Date)
            {
                return DateTime.Now.ToString("yyyyMMdd") + mytable[0].Seed;
            }

            return mytable[0].CurrentSequence;

        }


        /// <summary>
        /// computer md5 hash value
        /// </summary>
        /// <param name="oriStr">original string</param>
        /// <returns></returns>
        public static string ComputerMD5(string oriStr)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(oriStr));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();


        }

        public static string GetStockOutStat(int state)
        {
            string myRet="未知";
            switch (state)
            {
                case 1:
                    {
                        myRet = "生成";
                        break;
                    }
                case 2:
                    {
                        myRet = "审核";
                        break;
                    }
                case 3:
                    {
                        myRet = "完成";
                        break;
                    }
                case 4:
                    {
                        myRet = "取消";
                        break;
                    }
                case 5:
                    {
                        myRet = "部分完成";
                        break;
                    }
                case 6:
                    {
                        myRet = "已传送";
                        break;
                    }
            }

            return myRet;
        }

        private static Dictionary<string, string> mgrConfiguration = null;

        /// <summary>
        /// get management platiform configuration
        /// </summary>
        public static Dictionary<string, string> MgrConfiguration
        {
            get
            {
                if (mgrConfiguration==null)
                {
                    ConfigurationTableAdapter myAdapter = new ConfigurationTableAdapter();
                    var myTable = myAdapter.GetDataByType(1);

                    
                    mgrConfiguration=new Dictionary<string,string>(myTable.Count);

                    foreach (var myRow in myTable)
                    {
                        mgrConfiguration.Add(myRow.Title.ToLower(), myRow.Value.ToLower());
                    }
                }

                return mgrConfiguration;
            }
        }


        public const string ExceptionCaption = "错误";
    }
}
